﻿package igz.fleaxy.ui.form.checkbox 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import igz.fleaxy.ui.text.LabelUi;
	import igz.fleaxy.util.ObjectUtil;
	/**
	 * ...
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class CheckBoxUi extends Sprite
	{		
		protected var _LabelUi:LabelUi;
		public function get $LabelUi():LabelUi {
			return _LabelUi;
		}
		
		protected var _IsChecked:Boolean;
		public function get $IsChecked():Boolean {
			return _IsChecked;
		}
		public function set $IsChecked(p_bool:Boolean):void {
			if (_IsChecked != p_bool) {
				$Clicked();
			}
		}
				
		protected var _Value : String;
		public function get $Value() : String {
			return _Value;
		}
		
		protected var _CheckedUi	: Sprite;
		protected var _UncheckedUi	: Sprite;
		
		protected var _Settings		: Object;	
		
		protected var _OnChangeFunction : Function;
		
		/**
		 *  Crea un un checkbox, este checkbox tiene dentro un 
		 * @param	p_settings 	_settings = {
				"label" : " " -> texto para la etiqueta (aunque sea un espacio en blanco)
				,"labelStyle" : "" -> estilo de la etiqueta
				,"value" : null -> valor que tendrá el checkbox
				,"imgCheck" : null ->DisplayObject para cuando esta clikeado
				,"imgUncheck" : null ->DisplayObject para cuando no esta clikeador
				,"onChange" : null -> Función que se ejecutará cuando se realice un cambio
			}
		 */
		public function CheckBoxUi( p_settings:Object = null ) 
		{
			_Settings = {
				"label" 		: ""
			   ,"labelStyle" 	: ""
			   ,"value"			: ""
			   ,"imgCheck" 		: null
			   ,"imgUncheck" 	: null
			   ,"onChange"		: null
			   ,"checked"		: false
			}
			ObjectUtil.$Merge( p_settings, _Settings );
			
			_LabelUi = new LabelUi(_Settings["label"], _Settings["labelStyle"], p_settings);
			
			_CheckedUi 	= new Sprite();
			_CheckedUi.buttonMode 		= true;
			_CheckedUi.useHandCursor 	= true;
			_CheckedUi.mouseEnabled 	= true;			
			
			_UncheckedUi = new Sprite();
			_UncheckedUi.buttonMode 	= true;
			_UncheckedUi.useHandCursor 	= true;
			_UncheckedUi.mouseEnabled 	= true;			

			
			_Value = _Settings["value"];
			_OnChangeFunction = null;
			_Repaint();
			
			
			_LabelUi.x = _CheckedUi.width + 2;
			addChild(_LabelUi);
			
			//Damos tamaño proporcional al tamaño del label
			//_CheckedUi.height 	= _LabelUi.height - 4;
			//_CheckedUi.width 	= _LabelUi.height - 4;	
			//_UncheckedUi.height = _LabelUi.height - 4;
			//_UncheckedUi.width 	= _LabelUi.height - 4;
			
			addChild(_CheckedUi);
			addChild(_UncheckedUi);
			
			_CheckedUi.visible 	= false;
			_UncheckedUi.visible = false;
			
			_IsChecked = true;		
			
			$Clicked();	
			
			if (_Settings["onChange"] != null)
				_OnChangeFunction = _Settings["onChange"];
			else {
				_OnChangeFunction = null;
			}
		}
		
		protected function _Repaint() : void {
			var imgCheck   : Bitmap 	= _Settings["imgCheck"] as Bitmap;
			var imgUncheck : Bitmap 	= _Settings["imgUncheck"] as Bitmap;
			var isDefault:Boolean = ((imgCheck == null) || (imgUncheck == null));
			if (isDefault) {
				//Aplicamos estilo por defecto
				const CHECK_SIZE : uint = 15;
				_CheckedUi.graphics.lineStyle(1, 0);
				_CheckedUi.graphics.beginFill(0xFFFFFF);
				_CheckedUi.graphics.drawRoundRect( 0, 0, CHECK_SIZE, CHECK_SIZE, 2, 2);
				_CheckedUi.graphics.endFill();
				_CheckedUi.graphics.lineStyle(2, 0);
				_CheckedUi.graphics.moveTo(0, 0);
				_CheckedUi.graphics.lineTo(_CheckedUi.width-1, _CheckedUi.height-1);
				_CheckedUi.graphics.moveTo(0, _CheckedUi.height-1);
				_CheckedUi.graphics.lineTo(_CheckedUi.width-1, 0);

				
				_UncheckedUi.graphics.beginFill(0xFFFFFF);	
				_UncheckedUi.graphics.lineStyle(1, 0x000000);				
				_UncheckedUi.graphics.drawRoundRect(0, 0, CHECK_SIZE, CHECK_SIZE, 2, 2);
				_UncheckedUi.graphics.endFill();				
			}
			else {		
				_CheckedUi.addChild(imgCheck);
				_UncheckedUi.addChild(imgUncheck);				
			}
			
			_CheckedUi.y = _LabelUi.textHeight - _CheckedUi.height >> 1;
			_UncheckedUi.y = _LabelUi.textHeight - _UncheckedUi.height >> 1;
		}
		
		public function $Clicked():void {
			if (_IsChecked) {
				
				_IsChecked 			= false;
				_CheckedUi.visible 	= false;
				
				_CheckedUi.removeEventListener ( MouseEvent.CLICK, _IsClicked );				
				_UncheckedUi.visible = true;				
				_UncheckedUi.addEventListener ( MouseEvent.CLICK, _IsClicked );
				
			}else {
				
				_IsChecked 				= true;
				_UncheckedUi.visible 	= false;
				
				_UncheckedUi.removeEventListener ( MouseEvent.CLICK, _IsClicked );				
				_CheckedUi.visible = true;				
				_CheckedUi.addEventListener ( MouseEvent.CLICK, _IsClicked );					
			
			}
			if (_OnChangeFunction != null)
				_OnChangeFunction();
		}	
		
		protected function _IsClicked ( p_event : MouseEvent) : void {
			$Clicked();
		}
	}
}